package formula;

import atom.Atom;
import atom.UnaryAtom;
import literal.Literal;
import literal.UnaryLiteral;

import java.util.IllegalFormatCodePointException;
import java.util.Objects;

public class UnaryFormula extends Formula{
    UnaryLiteral literal;

    public UnaryFormula(UnaryLiteral literal){
        this.literal=literal;
    }

    @Override
    public String toString(){
        return literal.toString();
    }

    //实现成功啦！
    //判断是不是互斥（取反）
    @Override
    public boolean isNegated(Formula f) {
        if (f instanceof UnaryFormula){
            UnaryFormula f1=(UnaryFormula) f;
            Atom atom0=this.literal.getAtom();
            Atom atom1=f1.literal.getAtom();
            if (atom0.equals(atom1)){//same atom
                if (this.equals(f1)){//same negated
                    return false;
                }else {//same atom,different negated (is Negated)
                    return true;
                }
            }else {//different symbol
                return false;
            }
        }else if (f instanceof StarFormula){
            return true;
        }

        return false;
    }


    @Override
    //获得UnaryAtom的取反值
    public UnaryFormula getNegated(){
        Atom atom=this.literal.getAtom();
        boolean negated=this.literal.getNegated();//literal中的getNegated方法，返回一个bool值，atom是否带有负号
        if (negated){//true,说明atom带有负号，,返回值不携带负号
            UnaryFormula negated_f=new UnaryFormula(new UnaryLiteral(atom,false));
            return negated_f;
        }else {//false,atom不携带负号,返回值要携带负号
            UnaryFormula negated_f=new UnaryFormula(new UnaryLiteral(atom,true));
            return negated_f;
        }
    }

    //当前对象为unary formula时使用
    public boolean hasRelationWith(UnaryFormula other){
       char this0=this.toString().charAt(0);
       char other0=other.toString().charAt(0);
       if (this0==other0){
           return true;
       }else {return false;}
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (this instanceof StarFormula) return true;
        if (o instanceof StarFormula) return true;
        if (o == null || getClass() != o.getClass()) return false;
        UnaryFormula that = (UnaryFormula) o;
        return Objects.equals(literal, that.literal);
    }

    @Override
    public int hashCode() {
        return Objects.hash(literal);
    }



}
